package com.example.sxt_practise.thread;

/**
 * @author gaofeng
 * @date 2022年05月28日 10:54
 */
public class LockTest {
    public static void main(String[] args) {
        new MarkUp(0, "小红").start();
        new MarkUp(1, "小白").start();
    }
}

class LipStick {

}

class Mirror {

}

class MarkUp extends Thread {

    // static保证只有一个
    static LipStick lipStick = new LipStick() ;
    static Mirror mirror = new Mirror() ;

    int choice ;
    String girlName ;

    public MarkUp(int choice, String girlName) {
        this.choice = choice ;
        this.girlName = girlName ;
    }


    @Override
    public void run() {
        try {
            makeup();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void makeup() throws InterruptedException {
        if (choice == 0) {
            synchronized (lipStick) {
                System.out.println(this.girlName + "获得了口红的锁");
                Thread.sleep(1000);

                /*synchronized (mirror) {
                    System.out.println("想要获得镜子的锁");
                }*/
            }
            synchronized (mirror) {
                System.out.println("想要获得镜子的锁");
            }
        } else {
            synchronized (mirror) {
                System.out.println(this.girlName + "获得了镜子的锁");
                Thread.sleep(2000);

               /* synchronized (lipStick) {
                    System.out.println("想要获得口红的锁");
                }*/
            }
            synchronized (lipStick) {
                System.out.println("想要获得口红的锁");
            }
        }
    }
}
